package com.dede.sectionindexertest

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.SectionIndexer
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private val timeZones by lazy {
        val list = emptyList<String>().toMutableList()
        resources.getStringArray(R.array.timezone_labels)
                .forEach {
                    if (it.contains(",")) {
                        val split = it.split(",")
                        split.forEach {
                            list.add(it.trim())
                        }
                    } else {
                        list.add(it)
                    }
                }
        list.sort()
        list
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        list_view.scrollBarStyle = View.SCROLLBARS_INSIDE_INSET
        list_view.isFastScrollAlwaysVisible = true
        list_view.isFastScrollEnabled = true

        list_view.adapter = Adapter()
    }

    inner class Adapter : BaseAdapter(), SectionIndexer {

        private var sections: Array<String>? = null
        private var sectionsPosition: Array<Int>? = null
        private var sectionsIndexs: Array<Int>? = null

        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            val view = convertView ?: View.inflate(this@MainActivity, android.R.layout.simple_list_item_1, null)
            view.findViewById<TextView>(android.R.id.text1).text = timeZones[position]
            return view
        }

        override fun getItem(position: Int): String = timeZones[position]

        override fun getItemId(position: Int) = position.toLong()

        override fun getCount() = timeZones.size

        override fun getSections(): Array<Any>? {
            if (sections == null) {
                val starts = ArrayList<String>()
                val positions = ArrayList<Int>()
                val indexs = ArrayList<Int>()
                var start: String? = null
                var index = -1
                timeZones.forEachIndexed { i, str ->
                    val thisS = str.substring(0, 1)
                    if (start != thisS) {
                        starts.add(thisS.toUpperCase())
                        positions.add(i)

                        start = thisS
                        index++
                    }
                    indexs.add(index)
                }
                sections = starts.toArray(arrayOfNulls(starts.size))
                sectionsPosition = positions.toArray(arrayOfNulls(positions.size))
                sectionsIndexs = indexs.toArray(arrayOfNulls(indexs.size))
            }
            return sections as Array<Any>
        }

        override fun getSectionForPosition(position: Int): Int {
            return sectionsIndexs!![position]
        }

        override fun getPositionForSection(sectionIndex: Int): Int {
            return sectionsPosition!![sectionIndex]
        }

    }
}
